﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LoanCalculator.Common;
namespace LoanCalculator.WebUX.Modules
{
    public partial class UCMortgageView : System.Web.UI.UserControl
    {
        private LCService.LCServiceClient lcs = new LCService.LCServiceClient();

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        private void Calculate()
        {
            try
            {
                
                double amount = CommonHelper.EnsureDouble(PurchasePriceTextBox.Text);
                double months = (CommonHelper.EnsureDouble(YearsTextBox.Text) * 12);
                double rate = CommonHelper.EnsureDouble(InterestRateTextBox.Text);
                double downpayment = CommonHelper.EnsureDouble(DownPaymentTextBox.Text);
                double propertytax = CommonHelper.EnsureDouble(PropertyTaxTextBox.Text);
                double propertyinsurance = CommonHelper.EnsureDouble(PropertyInsuranceTextBox.Text);

                MonthlyPaymentTextBox.Text = lcs.CalculateMonthlyPayment(amount, rate, months, downpayment, propertytax, propertyinsurance).ToString("C");

                AmortizationGridView.DataSource = lcs.CalculateAmortization(amount, rate, months, downpayment, propertytax, propertyinsurance);
                AmortizationGridView.DataBind();
            }
            catch (Exception ex)
            {
                MajorError.Text = ex.Message;
            }
        }

        protected void CalculateButton_Click(object sender, EventArgs e)
        {
            Calculate();
        }

        protected void CancelButton_Click(object sender, EventArgs e)
        {
            PurchasePriceTextBox.Text = "";
            YearsTextBox.Text = "";
            InterestRateTextBox.Text = "";
            DownPaymentTextBox.Text = "";
            PropertyTaxTextBox.Text = "";
            PropertyInsuranceTextBox.Text = "";
        }

    }
}